package model

import "errors"

var(
	ErrNotFountBook = errors.New("没有找到这本书")
)

type Student struct {
	Name string
	Sex string
	Id string
	Grade string
	books []*BorrowItem
}

type BorrowItem struct {
	book *Book
	num int
}

func CreateStudent(name,sex,id,grade string) *Student{
	stu:= &Student{
		Name:name,
		Sex:sex,
		Id:id,
		Grade:grade,
	}
	return stu
}

func (s *Student) addBook(b *BorrowItem) {
	s.books = append(s.books,b)
}

func (s *Student) DelBook(b *BorrowItem) error {
	for i := 0; i < len(s.books); i++ {
		if (s.books[i].book.Name == b.book.Name) {
			if s.books[i].num == b.num {
				front := s.books[0:i]
				left := s.books[i+1:]
				s.books = append(front ,left...)
				return nil
			}
			s.books[i].num -= b.num
			return nil
		}
	}
	return ErrNotFountBook
}